package com.czl.module_main.viewmodel

import android.os.Bundle
import androidx.databinding.ObservableField
import androidx.databinding.ObservableInt
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.config.AppConstants
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.*
import com.czl.base.event.LiveBusCenter
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriber200Helper
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper

class HomeViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {

    val uc = UiChangeEvent()
    var areaName = ObservableField(model.getAreaName())
    var houseKeeperName = ObservableField("")
    var houseKeeperId = ""
    var houseKeeperPhone = ""
    var houseKeeperBuildNo = ObservableField("")
    var houseKeeperPhoto = ObservableField("")
    var score = ObservableInt(0)
    var showActivityTitle=ObservableField<Boolean>()
    var showNoticeTitle=ObservableField<Boolean>()
    var showKeeper=ObservableField<Boolean>()

    val onRefreshListener: BindingCommand<Void> = BindingCommand(BindingAction {
        getCommunityData()
        getNoticeData()
        queryHouseKeeperList()
    })
    class UiChangeEvent {
        val loadCompleteEvent: SingleLiveEvent<List<CarItem>> = SingleLiveEvent()
        val deleteResultEvent: SingleLiveEvent<Void> = SingleLiveEvent()
        val switchAreaEvent: SingleLiveEvent<Void> = SingleLiveEvent()
        val callEvent: SingleLiveEvent<String> = SingleLiveEvent()
        val loadActivityEvent: SingleLiveEvent<CommunityResponseBean> = SingleLiveEvent()
        val loadNoticeEvent: SingleLiveEvent<NoticeResponseBean> = SingleLiveEvent()
        val homeRefreshEvent:SingleLiveEvent<Void> = SingleLiveEvent()
    }

    val toastDoingEvnent: SingleLiveEvent<Void> = SingleLiveEvent()
    val btnToastCLick: BindingCommand<Void> = BindingCommand(BindingAction {
        toastDoingEvnent.call()
    })
    val btnToMoreCLick: BindingCommand<Void> = BindingCommand(BindingAction {
        LiveBusCenter.postToMoreEvent()
    })
    val btnMoreKeeperClick: BindingCommand<Void> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.Main.F_HOUSEKEEPERS)
    })
    val onSwitchAreaCommand: BindingCommand<Any> = BindingCommand(BindingAction {
        uc.switchAreaEvent.call()
    })
    val onNoticeCommand: BindingCommand<Any> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.Notice.F_NOTICE)
    })
    val onEvaluateClick: BindingCommand<Any> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.Report.F_SERVICE_SCORE,
            Bundle().apply {
                putString(AppConstants.BundleKey.EVALUATED_ID, houseKeeperId)
                putString(AppConstants.BundleKey.EVALUATED_NAME, houseKeeperName.get())
                putInt(
                    AppConstants.BundleKey.COMMENT_TYPE_KEY,
                    AppConstants.Constants.COMMENT_TYPE_HOUSE
                )
            })
    })

    val onCallClick: BindingCommand<Void> = BindingCommand(BindingAction {
        uc.callEvent.postValue(houseKeeperPhone)
    })
    val btnReportClick: BindingCommand<Void> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.Report.F_REPORT_SUBMIT)
    })
    val btnParkClick: BindingCommand<Void> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.Main.F_PARK_PAY_HOME)
    })
    val btnComplaintClick: BindingCommand<Void> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.Complaint.F_COMPLAINT_SUBMIT)
    })

    fun getMyQueryHistory() {
        model.getMyQueryHistory()
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<List<CarItem>>>() {
                override fun onResult(t: BaseBean<List<CarItem>>) {
                    if (t.code == 200) {
                        uc.loadCompleteEvent.postValue(t.data)
                    } else {
                        uc.loadCompleteEvent.postValue(null)
                    }
                }

                override fun onFailed(msg: String?) {
                    showErrorToast(msg)
                }

            })
    }

    fun getCommunityData() {
        model.getCommunities(CommunityRequestBean(model.getAreaId(), "", "", 0, 3))
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriber200Helper<BaseBean<CommunityResponseBean>>() {
                override fun onResult(t: BaseBean<CommunityResponseBean>) {
                    if (t.data != null) {
                        uc.loadActivityEvent.postValue(t.data)

                        if (t.data!!.records.size==0){
                            showActivityTitle.set(false)
                        }else{
                            showActivityTitle.set(true)
                        }
                    }
                    uc.homeRefreshEvent.call()
                }

                override fun onFailed(msg: String?) {
                    showErrorToast(msg)
                    showActivityTitle.set(false)
                    uc.homeRefreshEvent.call()
                }

            })
    }

    fun getNoticeData() {
        model.getNotices(CommunityRequestBean(model.getAreaId(), "", "", 0, 3))
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriber200Helper<BaseBean<NoticeResponseBean>>() {
                override fun onResult(t: BaseBean<NoticeResponseBean>) {
                    if (t.data != null) {
                        uc.loadNoticeEvent.postValue(t.data)
                        if (t.data!!.records.size==0){
                            showNoticeTitle.set(false)
                        }else{
                            showNoticeTitle.set(true)
                        }
                    }
                    uc.homeRefreshEvent.call()
                }

                override fun onFailed(msg: String?) {
                    showNoticeTitle.set(false)
                    showErrorToast(msg)
                    uc.homeRefreshEvent.call()
                }

            })
    }

    fun queryHouseKeeperList() {
        model.apply {
            queryHouseKeeperList(
                mapOf(
                    "areaNo" to model.getAreaId(),
                )
            )
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribe(object : ApiSubscriberHelper<BaseBean<HouseKeeperBean>>() {
                    override fun onResult(t: BaseBean<HouseKeeperBean>) {
                        if (t.code == 200) {
                            if (t.data==null){
                                showKeeper.set(false)
                            }else{
                                showKeeper.set(true)
                            }
                            t.data?.let {
                                houseKeeperName.set(it.name)
                                houseKeeperPhoto.set(it.facePhoto)
                                score.set(it.score)

                                houseKeeperPhone = it.phone
                                houseKeeperId = it.userId
                                houseKeeperBuildNo.set(it.buildingNo)
                            }
                        } else {
                            showNormalToast(t.msg)
                        }
                        uc.homeRefreshEvent.call()
                    }

                    override fun onFailed(msg: String?) {
                        showNormalToast(msg)
                        uc.homeRefreshEvent.call()
                    }

                })
        }
    }

}